
//现在的username
var current_username = $.cookie('username');

//性别选择
var  sexchange;
$(document).ready(function(){
    //读取登录状态
    var if_login=$.cookie("status");
    //读取用户名
    var username=$.cookie("username");
    if(if_login==1){
        $("#login_region").text(username);
        $("#dropdown-login-item").css("display","none");
        $("#dropdown-exit-item").css("display","block");
        
    }else if(if_login==0){
        $("#login_region").text("登录/注册");
        $("#dropdown-login-item").css("display","block");
        $("#dropdown-exit-item").css("display","none");
    }


    
    var data ={
        name:current_username,
    }
    $.ajax({
        type: 'GET',
        url: "http://8.130.103.251:8080/user/getuser", 
        data:data,  
        async: false,
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {   
            
            $("#user_name_show").text(data.data.username);
            $("#user_profile").attr("src",data.data.picture);
            $("#user_phone_show").attr("placeholder",data.data.phonenumber);
            $(".user-email-info").text(data.data.email);
            
            //判断性别
            if(data.data.sex==1){
                sexchange=1;
                $("#male").attr("checked",true);
                $("#female").attr("checked",false);
                
            }
            else if(data.data.sex==0){
                sexchange=0;
                $("#female").attr("checked",true);
                $("#male").attr("checked",false);
            }
        },
        error: function(xhr, type) {
            console.log("个人中心读取信息Error");
        }
     });

    //加载我的收藏
    var data1 ={
        userName:$.cookie('username'),
    }
    $.ajax({
        type: 'GET',
        url: "http://8.130.103.251:8080/Favour/getMyFavour", 
        data:data1,  
        async: false,
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {   
           
            for(var i=0;i<data.length;i++){
                var h1='<div class="recommend-content-item" data-id="'+data[i].goodid+'">';
                var h2='<a href="productDetails.html" target="detail-view" class="item-link">'+'<div class="img-wrapper">'+'<img  src="'+data[i].goodImg1+'">';
                var h3='</div>'+'<div class="info-wrapper">'+'<div class="title">'+data[i].goodName+'</div>'+'</div>';
                var h4='<div class="price-wrapper">'+'<span class="price-value">'+'<em>¥</em>'+'<span>'+data[i].monthPrice+'</span>';
                var h5='<span>/月</span>'+'</div>'+'</a>'+'<span class="favor-identi"><i class="fa fa-heart fa-2x" aria-hidden="true"></i></span>'+'</div>';
                var content=h1+h2+h3+h4+h5;
                $("#v-pills-collect").append(content);
            }
        },
        error: function(xhr, type) {
            console.log("个人中心我的收藏Error");
        }
     });

    //加载收货地址
    getuseraddr_func();
});

//重新加载收货地址
function getuseraddr_func(){
    $("#address-show-tbody").empty();
    var data={
        name:current_username,
    }
    $.ajax({
        type: 'GET',
        url: "http://8.130.103.251:8080/userad/getdata", 
        data:data,  
        async: false,
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {   
           
            for(var i=1;i<=data.data.length;i++){
                var h1='<tr '+'data-id='+data.data[i-1].id+'>'+ '<th scope="row">'+i+'</th>';
                var h2='<td>'+data.data[i-1].addressee+'</td>';
                var h3='<td>'+data.data [i-1].address+'</td>';
                var h4='<td>'+data.data[i-1].phonenumber+'</td>';
                var h5='<td class="adress-delete">'+'删除'+'</td>'+'</tr>';
                var content=h1+h2+h3+h4+h5;
                $("#address-show-tbody").append(content);
            }
        },
        error: function(xhr, type) {
            console.log("个人中心收获地址Error");
        }
     });
}



$("#male").click(function(){
    sexchange=1;
});

$("#female").click(function(){
    sexchange=0;
});


//保存修改按钮
$(".submit-button").click(function(){
    //正则匹配验证至少n位数字：
    var r=/^\d{11}$/; 
    var new_phone_number;
    if($("#user_phone_show").val()==""){
        new_phone_number=$("#user_phone_show").attr("placeholder");
        console.log(new_phone_number);
    }
    else if($("#user_phone_show").val()!==""){
        var rphone_exec = r.exec($("#user_phone_show").val())
        if(rphone_exec==null){
            alert("请输入正确的11位数字的电话号码");
        }
        else{
            new_phone_number=Number($("#user_phone_show").val());
            
        }

    }

    var data1={
        username:current_username,
        phonenumber:new_phone_number,
        sex:sexchange,
    }

    $.ajax({
        type: 'GET',
        url: "http://8.130.103.251:8080/user/changeuser", 
        data:data1,  
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {   
            console.log(data);   
        },
        error: function(xhr, type) {
            console.log("个人中心修改信息Error");
        }
     });

     var files = $('#getF').prop('files');
		 
		 var data = new FormData();
		 data.append('file', files[0]);
		 data.append('username',current_username);
		
		$.ajax({
		  url: 'http://8.130.103.251:8080/files/upload/user/oss',
		  type: 'POST',
		  data: data,
		  cache: false,
		  processData: false,
		  contentType: false,
		  success:function(res){
			console.log(JSON.stringify(res));
		  },
          error: function(xhr, type) {
            console.log("修改图片Error");
        }
		 });
   
});



//动态绑定收藏爱心的点击事件
$("#v-pills-collect").on("click",".favor-identi",function(){
    if (window.confirm('您确定要取消收藏该商品吗?')){

        var father_node=this.parentNode
        var product_id=father_node.getAttribute("data-id");
        father_node.parentNode.removeChild(father_node);
        var data={
            userName:current_username,
            productId:product_id,
        }
        $.ajax({
            type: 'GET',
            url: "http://8.130.103.251:8080/Favour/deleteFavour", 
            data:data,  
            async: false,
            ContentType: 'application/json;charset=utf-8',
            success: function(data) {   
               
            },
            error: function(xhr, type) {
                console.log("个人中心读取信息Error");
            }
         });
    }
});



//删除地址
$("#address-show-tbody").on("click",".adress-delete",function(){

    var id;
    if (window.confirm('您确定要删除该地址吗?')){
        var father_node=this.parentNode
        id=Number(father_node.getAttribute("data-id"));
    }
    var data={
        id:id,
    }
    $.ajax({
        type: 'POST',
        url: "http://8.130.103.251:8080/userad/deletebyid", 
        data:data,  
        async: false,
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {   
           alert(data.msg);
           getuseraddr_func();
        },
        error: function(xhr, type) {
            console.log("个人中心读取信息Error");
        }
     });
});

//添加地址
$("#add-address-btn").click(function(){

    //正则匹配验证至少n位数字：
    var r=/^\d{11}$/;
    var add_address;
    var consignee;
    var phone_number;

    if($("#address-info").val()==""){
        alert("收货地址不能为空！")
        return;
    }
    else{
        add_address=$("#address-info").val();
    }

    if($("#consignee-name").val()==""){
        alert("收货人姓名不能为空！")
        return;
    }
    else{
        consignee=$("#consignee-name").val();
    }

    if($("#consignee-phone").val()==""){
        alert("收货人电话不能为空！")
        return;
    }
    else if($("#consignee-phone").val()!==""){
        var rphone_exec = r.exec($("#consignee-phone").val())
        if(rphone_exec==null){
            alert("请输入正确的11位数字的电话号码");
            return;
        }
        else{
            phone_number=Number($("#consignee-phone").val());
            
        }
    }

    var data={
        name:current_username,
        address:add_address,
        addresses:consignee,
        phonenumber:phone_number,
    }
    $.ajax({
        type: 'POST',
        url: "http://8.130.103.251:8080/userad/uadadd", 
        data:data,  
        async: false,
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {  
            alert(data.msg);
            getuseraddr_func();
        },
        error: function(xhr, type) {
            console.log("个人中心收获地址Error");
        }
     });
});

$("#v-pills-myorder-tab").click(function(){
    window.location.href="order.html";
});


//导航栏顶部跳转数码专区
$("#digital-area").click(function(){
    $.cookie('pro_type',"数码配件",{ expires: 7 });
    window.open("tags.html");
});

//退出按钮的点击事件
$("#dropdown-exit-item").on("click",function () { 
    $.cookie('username', null);
    $.cookie('status', '0', { expires: 7 });
    window.location.href = "index.html";
    
})
